package com.app.controller.backstage;


import com.app.aspect.Log;
import com.app.common.Result;
import com.app.enums.StatusCodeEnum;
import com.app.model.AdminUserModel;
import com.app.model.UserModel;
import com.app.model.VisitRecordModel;
import com.app.service.AdminUserService;
import com.app.service.BrowseNumberService;
import com.app.service.VisitRecordsService;
import com.app.util.TokenUtils;
import com.app.vo.VisitRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//电话回访记录
@Slf4j
@Controller
@RestController
@RequestMapping("/jtwq/visit")
public class AdminVisitRecordsController {

    @Autowired
    VisitRecordsService visitRecordsService;
    @Autowired
    AdminUserService adminUserService;

    /**
     * 新增和修改电话回访记录
     */
    @PostMapping("/insertAndUpdateVisit")
    @ResponseBody
    @Log("编辑电话回访记录")
    public Result<Object> insertAndUpdateVisit(@RequestBody VisitRecordModel visitRecordModel,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        visitRecordsService.insertAndUpdateVisit(visitRecordModel);
        return Result.success("操作成功");
    }

    /**
     * 查询电话回访记录
     */
    @GetMapping("/getVisitRecordsList")
    @ResponseBody
    public Result<Object> getVisitRecordsList(@RequestParam Integer complaintId,
                                              @RequestParam String beginTime,
                                              @RequestParam String endTime,
                                              @RequestParam Integer page,
                                              @RequestParam Integer limit,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        VisitRecordVo visitRecordsList = visitRecordsService.getVisitRecordsList(complaintId,beginTime,endTime,page,limit);
        return Result.success(visitRecordsList);
    }

    /**
     * 删除电话回访记录
     */
    @GetMapping("/deleteVisitRecords")
    @ResponseBody
    @Log("删除电话回访记录")
    public Result<Object> deleteVisitRecords(@RequestParam Integer id,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");

        visitRecordsService.deleteVisitRecords(id);
        return Result.success("操作成功");
    }

    /**
     * 根据电话回访ID查询电话回访记录
     */
    @GetMapping("/getVisitRecordsById")
    @ResponseBody
    public Result<Object> getVisitRecordsById(@RequestParam Integer id,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        VisitRecordModel visitRecordModel = visitRecordsService.getVisitRecordsById(id);
        return Result.success(visitRecordModel);
    }


}
